package com.maple.exclusivesport.ui.main;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lorentzos.flingswipe.SwipeFlingAdapterView;
import com.maple.exclusivesport.DataBaseOpenHelper;
import com.maple.exclusivesport.R;
import com.maple.exclusivesport.SendEvent;
import com.maple.exclusivesport.adapters.ContactInfo;
import com.maple.exclusivesport.adapters.InnerAdapter;
import com.maple.exclusivesport.adapters.MyAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A placeholder fragment containing a simple view.
 */
public class Record2PlaceholderFragment extends Fragment implements SwipeFlingAdapterView.onFlingListener,
        SwipeFlingAdapterView.OnItemClickListener, View.OnClickListener {

    private static final String ARG_SECTION_NUMBER = "section_number";
    @Bind(R.id.swipe_view)
    SwipeFlingAdapterView swipeView;

    private int cardWidth;
    private int cardHeight;
    private InnerAdapter adapter;

    public static Record2PlaceholderFragment newInstance(int index) {
        Record2PlaceholderFragment fragment = new Record2PlaceholderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_SECTION_NUMBER, index);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            EventBus.getDefault().register(this);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_record2, container, false);
        ButterKnife.bind(this, root);


        initInfo();
        initView();

        return root;
    }

    private void initView() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        float density = dm.density;
        cardWidth = (int) (dm.widthPixels - (2 * 18 * density));
        cardHeight = (int) (dm.heightPixels - (338 * density));


        if (swipeView != null) {
            swipeView.setIsNeedSwipe(true);
            swipeView.setFlingListener(this);
            swipeView.setOnItemClickListener(this);

            adapter = new InnerAdapter(getContext(), cardHeight, cardWidth, mList);
            swipeView.setAdapter(adapter);
        }


    }


    @Override
    public void onItemClicked(MotionEvent event, View v, Object dataObject) {
    }

    @Override
    public void removeFirstObjectInAdapter() {
        adapter.remove(0);
    }

    @Override
    public void onLeftCardExit(Object dataObject) {
    }

    @Override
    public void onRightCardExit(Object dataObject) {
    }

    @Override
    public void onAdapterAboutToEmpty(int itemsInAdapter) {
        Log.i("tag", "itemsInAdapter" + itemsInAdapter);
        if (itemsInAdapter == 1) {
            initInfo();
        }
    }

    @Override
    public void onScroll(float progress, float scrollXProgress) {
    }


    List<ContactInfo> mList;


    private void initInfo() {


        if (mList == null)
            mList = new ArrayList<>();
        mList.addAll(DataBaseOpenHelper.getInstance(getContext()).queryContactInfoList());


        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        try {
            ButterKnife.unbind(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Send(SendEvent e) {
        Log.e("error", "send");
        mList.clear();
        initInfo();

    }

    @Override
    public void onClick(View v) {

    }
}